package com.hzw.saas.service.hpc.listener;

import cn.hutool.core.collection.CollectionUtil;
import com.hzw.saas.api.hpc.bo.HpcAccountBo;
import com.hzw.saas.service.hpc.event.HpcAccountAddEvent;
import com.hzw.saas.service.hpc.event.HpcAccountAllotEvent;
import com.hzw.saas.service.hpc.event.HpcAccountDeleteEvent;
import com.hzw.saas.service.hpc.event.HpcAccountUnAllotEvent;
import com.hzw.saas.service.hpc.listener.handler.QCHpcAccountHandlerImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author sonam
 * @sine 2021/8/25 10:45 上午
 */
@Slf4j
@Order(1)
@Component
@RequiredArgsConstructor
public class QCHpcAccountListener {

    @Resource(name = "qc")
    private QCHpcAccountHandlerImpl qcHpcAccountHandler;

    @EventListener(HpcAccountAddEvent.class)
    public void add(HpcAccountAddEvent hpcAccountAddEvent) {
        List<HpcAccountBo> hpcAccounts = hpcAccountAddEvent.getHpcAccounts();
        if(CollectionUtil.isEmpty(hpcAccounts)) {
            return;
        }
        hpcAccounts.forEach(hpcAccountBo -> {
            qcHpcAccountHandler.execute(hpcAccountBo, hpcAccountAddEvent);
        });
    }

    @EventListener(HpcAccountDeleteEvent.class)
    public void delete(HpcAccountDeleteEvent hpcAccountDeleteEvent) {
        qcHpcAccountHandler.execute(hpcAccountDeleteEvent.getHpcAccount(), hpcAccountDeleteEvent);
    }

    @EventListener(HpcAccountAllotEvent.class)
    public void allot(HpcAccountAllotEvent hpcAccountAllotEvent) {
        qcHpcAccountHandler.execute(hpcAccountAllotEvent.getHpcAccountBo(), hpcAccountAllotEvent);
    }

    @EventListener(HpcAccountUnAllotEvent.class)
    public void unAllot(HpcAccountUnAllotEvent hpcAccountUnAllotEvent) {
        qcHpcAccountHandler.execute(hpcAccountUnAllotEvent.getHpcAccountBo(), hpcAccountUnAllotEvent);
    }

}
